import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//模拟实现简单的线程池
class MyThreadPool{
    //使用一个阻塞队列存储任务
    private final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
    //把任务添加到队列里
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    public MyThreadPool(int num){
        //创建出n个线程，执行队列的任务
        for (int i = 0;i<num;i++){
            Thread t = new Thread(() ->{
                try {
                    Runnable runnable = queue.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
    }
}


public class demo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(4);
        for (int i = 1;i<=2;i++) {
            int tmp = i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello  "+tmp);
                }
            });
//            System.out.println(tmp);
        }



    }
}
